#pragma once

#include<cstdio>
#include<cstdlib>
#include<mysql/mysql.h>
#include<jsoncpp/json/json.h>

namespace order_system{


	static MYSQL* MySQLInit(){

		MYSQL* mysql = mysql_init(NULL);
		if (mysql_real_connect(mysql, "127.0.0.1", "root", "yjy", "order_system2", 3306, NULL, 0) == NULL){
			printf("mysql连接失败%s\n", mysql_error(mysql));
			return NULL;
		}

		mysql_set_character_set(mysql, "utf8");
		return mysql;
	}
	static void MySQLRelease(MYSQL* mysql){
		mysql_close(mysql);
	}

	class DishTable{
	public:
		DishTable(MYSQL* mysql) :mysql_(mysql){

		}
		bool Insert(const Json::Value& dish){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "insert into dish_table values(null,'%s',%d)", dish["name"].asCString(), dish["price"].asInt());
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("DishTable Insert failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("DishTable Insert Ok!\n");
			return true;
		}
		bool SelectAll(Json::Value* dishes){
			char sql[1024 * 4];
			sprintf(sql, "select dish_id,name,price from dish_table");
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("Dishtable SelectAll failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("DshTable SelectAll ok!");
			MYSQL_RES* result = mysql_store_result(mysql_);
			int rows = mysql_num_rows(result);
			for (int i = 0; i<rows; ++i){
				MYSQL_ROW row = mysql_fetch_row(result);
				Json::Value dish;
				dish["dish_id"] = atoi(row[0]);
				dish["name"] = row[1];
				dish["price"] = atoi(row[2]);
				dishes->append(dish);
			}
			mysql_free_result(result);
			return true;
		}
		bool SelectOne(int32_t dish_id, Json::Value* dish){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "select dish_id,name,price from dish_table where dish_id = %d", dish_id);
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("Dishtable SelectOne failed! %s\n", mysql_error(mysql_));
				return false;
			}
			MYSQL_RES* result = mysql_store_result(mysql_);
			int rows = mysql_num_rows(result);
			if (rows != 1){
				printf("DishTable SelectOne result rows not 1!");
				mysql_free_result(result);
				return false;
			}
			MYSQL_ROW row = mysql_fetch_row(result);
			(*dish)["dish_id"] = dish_id;
			(*dish)["name"] = row[1];
			(*dish)["price"] = atoi(row[2]);
			mysql_free_result(result);
			return true;
		}
		bool Update(const Json::Value& dish){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "update dish_table set name = '%s',price = %d where dish_id = %d", dish["name"].asCString(), dish["price"].asInt(), dish["dish_id"].asInt());
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("DishTable Update failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("DishTable UpDate ok!\n");
			return true;
		}
		bool Delete(int32_t dish_id){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "delete from dish_table where dish_id = %d", dish_id);
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("DishTable Delete failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("DishTable Delete ok!\n");
			return true;
		}
	private:
		MYSQL* mysql_;
	};

	class OrderTable{
	public:
		OrderTable(MYSQL* mysql) :mysql_(mysql){
		}
		bool Insert(const Json::Value& order){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "insert into order_table values(null,'%s','%s','%s','%d')", order["table_id"].asCString(), order["time"].asCString(), order["dishes"].asCString(), order["state"].asInt());
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("OrderTable Insert failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("OrderTable Insert OK! \n");
			return true;
		}
		bool SelectAll(Json::Value* orders){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "select order_id,table_id,time,dishes,status from order_table");
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("OrderTable SelectALL failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("OrderTable SelectALL OK! \n");
			MYSQL_RES* result = mysql_store_result(mysql_);
			int rows = mysql_num_rows(result);
			for (int i = 0; i<rows; ++i){
				MYSQL_ROW row = mysql_fetch_row(result);
				Json::Value order;
				order["order_id"] = atoi(row[0]);
				order["table_id"] = row[1];
				order["time"] = row[2];
				order["dishes"] = row[3];
				order["status"] = atoi(row[4]);
				orders->append(order);
			}
			mysql_free_result(result);
			return true;
		}
		bool ChangeState(const Json::Value& order){
			char sql[1024 * 4] = { 0 };
			sprintf(sql, "update order_table set status = '%d' where order_id=%d", order["status"].asInt(), order["order_id"].asInt());
			int ret = mysql_query(mysql_, sql);
			if (ret != 0){
				printf("OrderTable ChangeState failed! %s\n", mysql_error(mysql_));
				return false;
			}
			printf("OrderTable ChangeState OK! \n");
			return true;
		}
	private:
		MYSQL* mysql_;
	};
}//end order_system




